<?php
class ControllerExtensionPaymentAlipay extends Controller {

    const GATWAY_URL_DEV = "https://openapi.alipaydev.com/gateway.do"; // 测试的环境
    const GATWAY_URL_LIVE = "https://openapi.alipay.com/gateway.do";   // 生产的环境

	public function index() {
		$data['button_confirm'] = $this->language->get('button_confirm');

		$this->load->model('checkout/order');

		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);

		$config = array (
			'app_id'               => $this->config->get('payment_alipay_app_id'),
			'merchant_private_key' => $this->config->get('payment_alipay_merchant_private_key'),
			'notify_url'           => HTTP_SERVER . "payment_callback/alipay",
//			'notify_url'           => 'http://1199d733.ngrok.io'."/payment_callback/alipay",
			'return_url'           => $this->url->link('checkout/success'),
			'charset'              => "UTF-8",
			'sign_type'            => "RSA2",
//			'gateway_url'          => $this->config->get('payment_alipay_test') == "sandbox" ? "https://openapi.alipaydev.com/gateway.do" : "https://openapi.alipay.com/gateway.do",
			'alipay_public_key'    => $this->config->get('payment_alipay_alipay_public_key'),
		);
		$out_trade_no = trim($order_info['order_id']);
		$subject = trim($this->config->get('config_name'));
		$total_amount = trim($this->currency->format($order_info['total'], 'CNY', '', false));
		$body = '';//trim($_POST['WIDbody']);

        /**
         * @link https://docs.open.alipay.com/270/alipay.trade.page.pay - 接口文档
         *
         * out_trade_no - 商户订单号，64个字符以内、可包含字母、数字、下划线；需保证在商户端不重复（无需对支付宝不重复）
         */
		$payRequestBuilder = array(
			'body'         => $body,
			'subject'      => $subject,
			'total_amount' => $total_amount,
			'out_trade_no' => $out_trade_no,
			'product_code' => 'FAST_INSTANT_TRADE_PAY'
		);

		$this->load->model('extension/payment/alipay');

        $response = $this->model_extension_payment_alipay->pagePay($payRequestBuilder, $config);

        $action_url = $this->config->get('payment_alipay_test') == "sandbox" ? self::GATWAY_URL_DEV : self::GATWAY_URL_LIVE;
        $data['action'] = $action_url . "?charset=" . $this->model_extension_payment_alipay->getPostCharset();
        $data['form_params'] = $response;

		return $this->load->view('extension/payment/alipay', $data);
	}

	public function callback() {
		$this->log->write('alipay pay notify:');

		$arr = $_POST;
		$config = array (
			'app_id'               => $this->config->get('payment_alipay_app_id'),
			'merchant_private_key' => $this->config->get('payment_alipay_merchant_private_key'),
			'notify_url'           => HTTP_SERVER . "payment_callback/alipay",
//			'notify_url'           => 'http://1199d733.ngrok.io'."/payment_callback/alipay",
			'return_url'           => $this->url->link('checkout/success'),
			'charset'              => "UTF-8",
			'sign_type'            => "RSA2",
//			'gateway_url'          => $this->config->get('payment_alipay_test') == "sandbox" ? "https://openapi.alipaydev.com/gateway.do" : "https://openapi.alipay.com/gateway.do",
			'alipay_public_key'    => $this->config->get('payment_alipay_alipay_public_key'),
		);
		$this->load->model('extension/payment/alipay');
		$this->log->write(json_encode($_POST));
		$this->log->write('POST' . var_export($_POST,true));
        /**
         * @uses \ModelExtensionPaymentAlipay::check
         */
		$result = $this->model_extension_payment_alipay->check($arr, $config);

//		if($result || $this->request->post['debug']) {//check successed
		if($result) { //check successed
			$this->log->write('Alipay check successed');
			$order_id = $_POST['out_trade_no'];
			if($_POST['trade_status'] == 'TRADE_FINISHED') { // 不会触发回调通知

			} else if ($_POST['trade_status'] == 'TRADE_SUCCESS') { // 触发回调通知
				$this->load->model('checkout/order');
				$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_alipay_order_status_id'));
			}
			echo "success";	//Do not modified or deleted
		}else {
			$this->log->write('Alipay check failed');
			//chedk failed
			echo "fail";
		}
	}
}